﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Utility;
using Telerik.Web.UI;
using SalePartial.BLL;
using SalePartial.BLL.SAServiceRef;
using System.IO;

public partial class Admin_SAClient : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ////修正导航栏
            //HyperLink hlkHome = (HyperLink)Master.FindControl("hlkHome");
            //hlkHome.CssClass = "";

            //HyperLink hlkPartList = (HyperLink)Master.FindControl("hlkPartList");
            //hlkPartList.CssClass = "";

            //HyperLink hlkInquires = (HyperLink)Master.FindControl("hlkInquires");
            //hlkInquires.CssClass = "";

            //HyperLink hlkPublished = (HyperLink)Master.FindControl("hlkPublished");
            //hlkPublished.CssClass = "";

            //HyperLink hlkManagement = (HyperLink)Master.FindControl("hlkManagement");
            //hlkManagement.CssClass = "Selection";
        }
    }

    protected void radMenuNavi_ItemClick(object sender, RadMenuEventArgs e)
    {
        string radalertscript = "";

        switch (((RadMenu)sender).SelectedItem.Value)
        {
            case "login":
                break;
            case "logout":
                break;
            case "cmdFamilyPublish":
                break;
            case "cmdEdit":
                break;
            case "cmdFamilyDetail":
                break;
            case "downloadDoc":
                //radalertscript = "<script language='javascript'>function f(){var win = window.radopen('DownLoadFrm.aspx','Details');win.setSize(600, 300); win.center(); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
                familyDocDown();
                break;
            case "downloadFamily":
                //radalertscript = "<script language='javascript'>function f(){var win = window.radopen('FamilyDownFrm.aspx','Details');win.setSize(900, 700); win.center(); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
                break;
            case "about":
                break;
            default:
                break;

        }
    }
    protected void RadToolBarButton_ButtonClick(object sender, RadToolBarEventArgs e)
    {
        string radalertscript = "";
        if (!(e.Item is RadToolBarButton))
        {
            return;
        }
        switch (((RadToolBarButton)e.Item).CommandName)
        {
            case "login":
                break;
            case "logout":
                break;
            case "detail":
                break;
            case "downloadDoc":
                //radalertscript = "<script language='javascript'>function f(){var win = window.radopen('DownLoadFrm.aspx','Details');win.setSize(600, 300); win.center(); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                //  Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
                familyDocDown();
                break;
            case "downloadFamily":
                //radalertscript = "<script language='javascript'>function f(){var win = window.radopen('FamilyDownFrm.aspx','Details');win.setSize(900, 700); win.center();Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                //  Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
                break;
            case "familyEdit":
                break;
            case "familyDelete":
                break;
            case "publish":
                break;
            case "cateEdit":
                break;
            case "vendorEdit":
                break;
            case "standardEdit":
                break;
            case "help":
                break;
            default:
                break;
        }
    }

    private bool familyDocDown()
    {
        Guid familyId;
        if (HttpContext.Current.Session["familyId"] != null)
        {
            familyId = new Guid(HttpContext.Current.Session["familyId"].ToString());

        }
        else
        {
            SABLL.ShowInfoMsg(this.Page,@"无法下载，请在零件列表中至少选中一个零件！");
            return false;
        }
        FamilyAttrInfo familyAttrInfo = SABLL.GetFamilyAttrInfoByType(familyId,
            FamilyAttrType.kReference);

        if (familyAttrInfo != null && !String.IsNullOrEmpty(familyAttrInfo.ReferenceName) &&
            familyAttrInfo.ReferenceBinary != null && familyAttrInfo.ReferenceBinary.Length > 0)
        {
            //string path = HttpRuntime.AppDomainAppPath + "App_Data/"+familyAttrInfo.ReferenceName;
            //string referencePath = SABLL.SaveBinaryByPath(path, familyAttrInfo.ReferenceBinary);
            //string uri = HttpRuntime.AppDomainAppVirtualPath + "App_Data/" + familyAttrInfo.ReferenceName;
            //if (!String.IsNullOrEmpty(path))
            //{
            //远程下载文件 小文件
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + familyAttrInfo.ReferenceName);
            HttpContext.Current.Response.BinaryWrite(familyAttrInfo.ReferenceBinary);
            Response.Flush();
            Response.Close();
            SABLL.ShowInfoMsg(this.Page, String.Format("保存文档成功！"));
            //}
            //    link.NavigateUrl = "~/SAClientWeb/download.aspx?fn=" + referencePath;
            //    string radalertscript = "<script language='javascript'>function f(){var win = window.radopen('FamilyDownFrm.aspx','Details');win.setSize(800, 600); win.center();Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
            //    Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);

            //    if (File.Exists(referencePath))
            //    {
            //        File.Delete(referencePath);
            //    }
            //    SABLL.ShowInfoMsg(this.Page, String.Format("保存文档成功！"));
            //}
        }
        else
        {
            //no referece document.
            SABLL.ShowInfoMsg(this.Page, @"无可下载的文档资料！");
        }
        return true;
    }
}
